/**
 * ServerServiceImpl.java
 * com.sitech.system.server.service.impl
 * Function： TODO 
 *
 *   ver  author   date    
 * ──────────────────────────────────
 *   	 geutopia  Feb 12, 2011        
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
*/

package com.sitech.server.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import com.sitech.server.ServerConst;
import com.sitech.server.dao.ServerDao;
import com.sitech.server.domain.ServerInfo;
import com.sitech.server.service.ServerService;

/**
 * ClassName:ServerServiceImpl
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO ADD REASON
 *
 * @author   geutopia
 * @version  
 * @since    Ver 1.1
 * @Date	 2011	Feb 12, 2011		2:05:21 PM
 *
 * @see 	 
 */

public class ServerServiceImpl implements ServerService
{

	private static ServerInfo LOCAL_SERVER = null;
	private ServerDao serverDao; 
	
	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#getLocalServer()
	 */
	public synchronized ServerInfo getLocalServer()
	{
			if (ServerServiceImpl.LOCAL_SERVER == null)
			{
				ServerInfo serverInfo = new ServerInfo();
				serverInfo.setLastUpTime(new Date());				
				serverInfo.setServerIp(getLocalServerIp());
				serverInfo.setServerPort(getLocalServerPort());
				serverInfo.setServerDesc(getLocalServerDes());
				serverInfo.setServerName(getLocalServerName());
				ServerServiceImpl.LOCAL_SERVER = serverInfo;
			}
		return ServerServiceImpl.LOCAL_SERVER;
	}

	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#getServer(java.lang.String)
	 */
	public List<ServerInfo> getServer(String ip)
	{
		ServerInfo serverInfo = new ServerInfo();
		serverInfo.setServerIp(ip);
		return serverDao.getServer(serverInfo);
	}

	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#getServer(java.lang.String, java.lang.String)
	 */
	public List<ServerInfo> getServer(String ip, String port)
	{
		ServerInfo serverInfo = new ServerInfo();
		serverInfo.setServerIp(ip);
		serverInfo.setServerPort(port);
		return serverDao.getServer(serverInfo);
	}

	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#getServer(java.lang.String, java.lang.String, java.lang.String)
	 */
	public ServerInfo getServer(String ip, String port, String context)
	{
		ServerInfo serverInfo = new ServerInfo();
		serverInfo.setServerIp(ip);
		serverInfo.setServerPort(port);
		serverInfo.setServerContext(context);
		List<ServerInfo> list =  serverDao.getServer(serverInfo);
		if(list != null && list.size() > 0)
		{
			return list.get(0);
		}
		else
		{
			return null;
		}
	}

	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#insertServer(com.sitech.system.server.domain.ServerInfo)
	 */
	public void insertServer(ServerInfo serverInfo)
	{
		serverDao.insertServer(serverInfo);
	}

	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#updateServer(com.sitech.system.server.domain.ServerInfo)
	 */
	public void updateServer(ServerInfo serverInfo)
	{
		serverDao.updateServer(serverInfo);
	}
	
	protected String getLocalServerName()
	{
		String serverName = System.getProperty(ServerConst.SERVER_NAME_PROPERTY);
		return serverName;
	}
	
	protected String getLocalServerPort()
	{
		String serverPort = System.getProperty(ServerConst.SERVER_PORT_PROPERTY);
		if(serverPort == null || "".equals(serverPort.trim()))
		{
			serverPort = "80";
		}
		return serverPort;
	}
	
	protected String getLocalServerDes()
	{
		String serverDes = System.getProperty(ServerConst.SERVER_DESC_PROPERTY);
		return serverDes;
	}
	
	protected String getLocalServerIp()
	{
		try
		{
			return InetAddress.getLocalHost().getHostAddress();
		}
		catch (UnknownHostException e)
		{
			e.printStackTrace();
		}
		return "0.0.0.0";
	}

	public ServerDao getServerDao()
	{
		return serverDao;
	}

	public void setServerDao(ServerDao serverDao)
	{
		this.serverDao = serverDao;
	}

	/**
	 * (non-Javadoc)
	 * @see com.sitech.system.server.service.ServerService#setLocalServer(com.sitech.system.server.domain.ServerInfo)
	 */
	public void setLocalServer(ServerInfo serverInfo)
	{
		ServerServiceImpl.LOCAL_SERVER = serverInfo;
	}

	public List<ServerInfo> queryAllServerList()
	{
		return serverDao.queryAllServerList();
	}

}
